/*  
* Copyright (c) 2010, James Daniello
* This project is licensed under the 'New BSD' license.
* The full text of the license can be found in the root
* directory under the filename "LICENSE"
*/
package src.engine;

public class PICAIUAnimation{
  
  String anim;
  int frame, maxFrame;
  long elapsed,dur;
  boolean loop=true;
  boolean active=true;
  boolean finished=false;

  int offX=0,offY=0;
  
  /**
  * This class houses the information for an animation. It will cycle through the avaiable
  * animations and change the string it returns for the getAnimation() method.
  * @param animation the base name of the animation. This is the same as the one provided
  * in the animations.txt file
  * @param frames the number of frames this animation has
  * @param duration the time before the next frame is played
  */
  public PICAIUAnimation(String animation, int frames, long duration){
    anim=animation;
    maxFrame=frames-1;
    dur=duration;
  
  }

  /**
   * sets the animation's frame to the first one in the series.
   */
  public void restart(){
      frame=0;
      active=true;
      finished=false;
  }

  /*
   * Set's whether the animation should loop or not.
   */
  public void setLoop(boolean l){
      loop = l;
  }

  /*
   * Set's whether the animation is active or not
   */
  public void setActive(boolean l){
      active = l;
  }
  /**
  * Updates the current animation, jumping to the next frame as needed
  */
  public void update(long deltaT){
    if(active&&maxFrame>-1){
      elapsed+=deltaT;
      while(elapsed>dur){
        elapsed-=dur;
        frame++;
        if(frame>maxFrame){
            if(loop){
                frame=0;
            }else{
                frame--;
                active=false;
                finished=true;
            }
        }
      }
    }
  }
  
  /**
  * Returns the name of the animation to be pulled from the animation bank
  */
  public String getAnimation(){
    if(maxFrame>-1){
      return anim+frame;
    }else{
      return anim;
    }
  }

  /**
  * Returns a copy of this animation
  */
  public PICAIUAnimation clone(){
    PICAIUAnimation animR = new PICAIUAnimation(anim,maxFrame,dur);
    animR.frame=frame;
    animR.elapsed=elapsed;
    animR.active=active;
    animR.loop=loop;
    animR.offX=offX;
    animR.offY=offY;
    return animR;
  }


  public void setOffX(int x){
      offX=x;
  }

  public void setOffY(int y){
      offY=y;
  }

  public int getOffX(){
      return offX;
  }

  public int getOffY(){
      return offY;
  }

    public boolean hasFinished() {
        return finished;
    }

    public void setFrame(int f) {
        frame = f;
    }
}